<template>
    <div>

    </div>
</template>

<script>
    export default {
        name: "BaseEdit",
        data(){
            return {
                formData: {},
                ruleValidate:{},
                spinShow:false
            }
        },
        methods:{
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            load(url,data,isById,callback){
                this.handleReset('formData');
                if(data.id==null && isById==null){
                    this.formData={};
                    return ;
                }
                this.spinShow=true;
                this.$axios.post(url,this.$qs.stringify(data)).then(res=>{
                    let code = res.data.code;
                    let data = res.data.data;
                    if(code==200){
                        if(data==null){
                            this.formData={};
                        }else{
                            this.formData=data;
                        }
                        this.spinShow=false;
                        if(callback!=null){
                            callback();
                        }
                    }else{
                        this.$Message.error(data);
                    }
                }).catch(()=>{
                    this.$Message.error('服务器未响应!');
                });
            },save (name,url,callback) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        //this.spinShow=true;
                        this.$Modal.confirm({
                            title: '提示',
                            content: '<p>确定提交?</p>',
                            onOk: () => {
                                this.$axios.post(url,this.$qs.stringify(this.formData)).then(res=>{
                                    let code = res.data.code;
                                    let data = res.data.data;
                                    if(code==200){
                                        this.$Message.success('保存成功!');
                                        this.$emit("saveSucces");
                                        if(callback!=null){
                                            callback();
                                        }
                                    }else{
                                        this.$Message.error(data);
                                        // this.spinShow=false;
                                    }
                                }).catch(()=>{
                                    this.$Message.error('服务器响应错误!');
                                    // this.spinShow=false;
                                }).finally( ()=>{
                                });
                            },
                            onCancel: () => {

                            }
                        });
                    } else {
                        this.$Message.error('请输入必填项!');
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
